<template>
  <div>
    <!-- show-checkbox：tree 显示复选框
      check-strictly 父子节点不关联
      如果需要通过 key 获取已勾选的节点内容，需要设置 node-key
      获取已勾选的 keys ，调用this.$refs.tree.getCheckedKeys()
      -->
    <el-tree
      ref="tree"
      :data="permissionData"
      :props="{label:'name'}"
      show-checkbox
      check-strictly
      default-expand-all
      node-key="id"
    />
    <div class="footer">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import { getRoleDetail, assignPermission } from '@/api/setting'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: []
    }
  },
  created() {
    this.loadPermissions()
    this.loadRoleDetail()
  },
  methods: {
    async loadPermissions() {
      const res = await getPermissionList()
      this.permissionData = tranListToTreeData(res.data)
    },

    async  hSubmit() {
      const ids = this.$refs.tree.getCheckedKeys()
      const res = await assignPermission({
        id: this.id, permIds: ids
      })
      this.$message.success(res.message)
      this.$emit('close')
      console.log(ids)
    },
    async loadRoleDetail() {
      const res = await getRoleDetail(this.id)
      //   console.log(res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    }
  }
}
</script>
<style lang="scss" scoped>
.footer {
  display: flex;
  justify-content: flex-end;
}
</style>
